import requests

"""
建议:每次打开浏览器使用系统时,最好将 Referer,Cookie 重新复制一份
未解决: 每次重启浏览器无法自动获取请求头
       转派意见为空时:最终结果会打印'转派意见:None'
"""

# 1.首页单号等信息

headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3947.100 Safari/537.36',
    'Referer':'http://10.62.233.178:8088/fwbz/ui/list/trouble?tag=WLGD0001002&disbtn=3&distransfer=3&disdealwith=3&disfeedback=3&disyupan=3&disguanlian=3&disordertop=3&ttType=2&funcId=120680093116760547175111&ttType=',
    # 重新登录后也不会变

    'Cookie': 'JSESSIONID=F2C06ED21F47232D840BB0157E8B00D2; rememberMe=e0NvlIrR24Zbfbl/2renewXf+vKMRdqeYOI6pTCu4uKNPVaBA49M/9nhkz7aWCQTjesrG87kudEuttr+2tUOJcDOnM0bc/M0dGLro9KK+rmuJL7/vPDBHRbhr5j2+W/y9mCj0ZsijEZp5ER1PzQP8kq3VaZ7h1jnrqGEkdEpHmbCko9Gi+Su7NFaXb5LEfwSgc23yNfh/fQe6+dw6NMM17/x7lQULNlukIauNTPy+tVH8TrqbA3DAhPA2EnvqxM5tVyVMWivvLXXfwBDi7qJt409TpCHFQTTMFjyoGUsjl2o7Sbm0wKKgXGbNBLPZzQ/MFQ/BKJuIro7jeMO6h+t1VebSkuJch92rm7ZDHG47oGYl1p8wQr2KKvoXXDw1qcdIPnB82yczbYLao6OOMVIrrZ1sRVMihliiqajw0yAMnCOQAhxuN6PIeGVmE+W4wkRtGPnO6vAV8V6ndlIRzvQYq5Ds5jk4AzsfJE5y7RSPDf+8s4S+6Zum4ymJQSPHwBYBYdAEpNiVSC9cne8+C8iYA=='

}

url = 'http://10.62.233.178:8088/fwbz/data/list/trouble'

data = {
    'tag': 'WLGD0001002',
    'order': 'asc',
    'offset': '0',    # 从id未几的工单开始爬取,
    'limit': '20',     # 每页获取多少张
    'conditionStr': '[{"name":"RECEIVE_DEPT","value":"电信维护1区"},{"op":"%like%","value":""},{"name":"","op":"in","value":""}]', # 获取不同账号工单,只需更改value,无需密码
    'listTag': '',
}

data_list = requests.post(url,headers=headers,data=data).json()      # 获取首页20张工单的  页面  信息字典
rows = data_list['rows']                                             # 获取首页20张工单中的 工单详情 字典
new_dic = {}                                                         # 新建一个空字典,后边将'故障单'作为键,'故障信息'和'转派意见'作为值
fp = open('C:/Users/SHOW-CC/Desktop/未恢复工单列表(DXWH1).txt', 'w', encoding='utf-8')         # 打开.txt文件准备开始写内容
for dic in rows:                                                     # 获取首页20张工单中的 工单号,站名,告警发生时间,bttid
    btt = dic['FLDID']


    # 2.故障单详情中的故障单号和故障详情
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3947.100 Safari/537.36',
        'Referer': 'http://10.62.233.178:8088/fwbz/ui/detail/trouble?tag=WLGD0001002&id='+btt+'&child=undefined&pTabId=tab_120680093116760547175111',
        # 更换故障单号,改变Btt,重新登录系统也不改变

        'Cookie': 'JSESSIONID=F2C06ED21F47232D840BB0157E8B00D2; rememberMe=e0NvlIrR24Zbfbl/2renewXf+vKMRdqeYOI6pTCu4uKNPVaBA49M/9nhkz7aWCQTjesrG87kudEuttr+2tUOJcDOnM0bc/M0dGLro9KK+rmuJL7/vPDBHRbhr5j2+W/y9mCj0ZsijEZp5ER1PzQP8kq3VaZ7h1jnrqGEkdEpHmbCko9Gi+Su7NFaXb5LEfwSgc23yNfh/fQe6+dw6NMM17/x7lQULNlukIauNTPy+tVH8TrqbA3DAhPA2EnvqxM5tVyVMWivvLXXfwBDi7qJt409TpCHFQTTMFjyoGUsjl2o7Sbm0wKKgXGbNBLPZzQ/MFQ/BKJuIro7jeMO6h+t1VebSkuJch92rm7ZDHG47oGYl1p8wQr2KKvoXXDw1qcdIPnB82yczbYLao6OOMVIrrZ1sRVMihliiqajw0yAMnCOQAhxuN6PIeGVmE+W4wkRtGPnO6vAV8V6ndlIRzvQYq5Ds5jk4AzsfJE5y7RSPDf+8s4S+6Zum4ymJQSPHwBYBYdAEpNiVSC9cne8+C8iYA=='
        #   cookie 跟步骤1的一样
    }

    url = 'http://10.62.233.178:8088/fwbz/fwbz/checkJiTuanOrder?ttid='+btt      # 不同的故障单只需更改BTT,重启系统一样

    data_list2 = requests.post(url,headers=headers).json()
    data2 = data_list2['data']               # 获取1张工单详情中上半部分的字典
    # for dic2 in data:                      # 获取1张工单详情中的 工单号,告警详情
    #     number = dic['TT_NUMBER']
    #     name = dic['DESCRIPTION']
    #     dtime = str(dic['OSS_CREATE_TIME'])
    number = data2[0]['TT_NUMBER']           # 获取工单号
    name = data2[0]['DESCRIPTION']           # 获取告警信息
    new_name = '故障信息: ' + name             # 获取告警信息


    # 3.故障单详情中的转派意见及反馈信息
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3947.100 Safari/537.36',
        'Referer': 'http://10.62.233.178:8088/fwbz/ui/detail/trouble?tag=WLGD0001002&id='+btt+'&child=undefined&pTabId=tab_120680093116760547175111',
        'Cookie': 'JSESSIONID=F2C06ED21F47232D840BB0157E8B00D2; rememberMe=e0NvlIrR24Zbfbl/2renewXf+vKMRdqeYOI6pTCu4uKNPVaBA49M/9nhkz7aWCQTjesrG87kudEuttr+2tUOJcDOnM0bc/M0dGLro9KK+rmuJL7/vPDBHRbhr5j2+W/y9mCj0ZsijEZp5ER1PzQP8kq3VaZ7h1jnrqGEkdEpHmbCko9Gi+Su7NFaXb5LEfwSgc23yNfh/fQe6+dw6NMM17/x7lQULNlukIauNTPy+tVH8TrqbA3DAhPA2EnvqxM5tVyVMWivvLXXfwBDi7qJt409TpCHFQTTMFjyoGUsjl2o7Sbm0wKKgXGbNBLPZzQ/MFQ/BKJuIro7jeMO6h+t1VebSkuJch92rm7ZDHG47oGYl1p8wQr2KKvoXXDw1qcdIPnB82yczbYLao6OOMVIrrZ1sRVMihliiqajw0yAMnCOQAhxuN6PIeGVmE+W4wkRtGPnO6vAV8V6ndlIRzvQYq5Ds5jk4AzsfJE5y7RSPDf+8s4S+6Zum4ymJQSPHwBYBYdAEpNiVSC9cne8+C8iYA=='
    }
    # 更换单号时,只更换单号 和BTT号
    url = 'http://10.62.233.178:8088/fwbz/fwbz/operinfos?tt_number='+number+'&isauto=&xianyuan=&tag=WLGD0001002&fldid='+btt+'&loginUser=DXWH1&orderules=&_=1629713461631'    # 用户名和最后的13位数字,可以随意更改或者写死

    data_list3 = requests.post(url,headers=headers).json()       # 获取多个  转派意见  的字典列表

    # 获取转派意见信息
    for data3 in data_list3:             # 循环 转派意见 所在下半部分字典,输出结果为,该站工单所在账号的所有`操作记录`
        data4 = data3['OPERATION']       # 'OPERATION'关键词封装
        data5 = data3['RESULT_INFO']     # 'RESULT_INFO'关键词的封装 = 'OPERATION'关键词对应的内容
        data6 = '转派处理意见: ' + str(data5)
        if data4 == '转派':               # 判断字典中'OPERATION'关键字是否为'转派',是则字典中存在 转派处理意见////亦可以根据恢复时间做判断获取不获取
            # if not data5 is None:      # 判断 转派处理意见是否为空
            LST = [new_name, data6]  # 新建列表,故障信息在前,转派意见在后
            new_dic[number] = LST    # 将新建字典中的列表所对应的信息作为值,赋值给'故障单号'的键
            print(number + ',获取成功!')

for k, v in new_dic.items():
    fp.write(k + '\n')      # 将获取的'故障单号'写到文档中
    for i in v:
        fp.write(i + '\n')  # 将获取的'故障信息'和'转派意见'写到文档中
    fp.write('\n')

fp.close()  # 关闭文档














